//路由相关配置,得到一个路由实例
import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由组件
const Login = () => import('@/views/login')
const Layout = () => import ('@/views/layout.vue')
const Welcome = () => import('@/views/welcome')
const Article = () => import('@/views/article')
const Image = () => import('@/views/image')
const Publish = () => import ('@/views/publish')
const Comment = () => import ('@/views/comment')
const Fans = () => import ('@/views/fans')
const Setting = () => import  ('@/views/setting')
const NotFound = () => import('@/views/404')


// 引入工具模块
import auth from '@/utils/auth.js'
Vue.use(VueRouter);

const routes = [
    {path:'/login',component:Login}
    ,{path:'/',component:Layout,
        children:[
            {path:'/',component:Welcome},
            // 文章管理
            {path:'/article',component:Article},
            // 素材
            {path:'/image',component:Image},
            // 编辑和发布文章
            {path:'/publish',component:Publish},
            // 评论
            {path:'/comment',component:Comment},
            // 粉丝管理
            {path:'/fans',component:Fans},
            // 个人设置
            {path:'/setting',component:Setting}
        ]
    },
    {path:'*',component:NotFound}
]
const router = new VueRouter({routes});
// 设置导航守卫
router.beforeEach((to,from,next) => {
    // 对象结构获取用户信息
    const {getUser} = auth;
    // 除了登录,都需要判断token
    if (to.path !== '/login' && !getUser().token) return next('/login')
    next();
})
export default router